package com.letoken.platform.app.service.transfer;

import com.letoken.platform.pub.constant.LetokenTokenConstant;
import com.letoken.platform.pub.enums.LetokenApiResponseEnum;
import com.letoken.platform.pub.enums.LetokenTokenTypeEnum;
import com.letoken.platform.pub.exception.ApiServiceException;
import com.letoken.platform.repo.app.dao.AppUserMapper;
import com.letoken.platform.repo.app.dao.TokenMapper;
import com.letoken.platform.repo.app.manager.AppUserLockedManager;
import com.letoken.platform.repo.app.manager.AppUserManager;
import com.letoken.platform.repo.app.po.AppUser;
import com.letoken.platform.repo.app.po.Token;
import com.letoken.platform.repo.app.redis.AppWalletBlackListRedis;
import com.letoken.platform.repo.app.redis.AppWalletRateLimitRedis;
import com.letoken.platform.repo.app.req.token.WithdrawUsdtReq;
import com.letoken.platform.repo.utlis.LocalBlockUtils;
import com.letoken.platform.repo.utlis.UserLockedTool;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.aop.framework.AopContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.time.LocalDateTime;

public abstract class AbstractTransfer implements ITransfer{
    @Autowired
    AppUserManager appUserManager;
    @Autowired
    AppUserLockedManager appUserLockedManager;
    @Autowired
    TokenMapper tokenMapper;


    @Override
    public ITransfer choose(Integer mode) {
        return null;
    }

    @Override
    public ITransfer checkout(WithdrawUsdtReq req) {
        if (req.getAmount().scale() > LetokenTokenConstant.AMOUNT_SCALE) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_WALLET_WITHDRAW_020);
        }
        AppUser user = appUserManager.queryMe();
        if (ObjectUtils.isEmpty(user)){
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_WALLET_WITHDRAW_004);
        }
        req.setUser(user);

        final String userStr = String.valueOf(req.getUser().getId());
        // 黑名单
        if (AppWalletBlackListRedis.me().getValue(userStr)) {
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_WALLET_WITHDRAW_017);
        }

        // 频率限制
        if (AppWalletRateLimitRedis.me().isLimited(userStr, "withdraw", 1)) {
            // 加入黑名单
            AppWalletBlackListRedis.me().setValue(userStr, true);
            // 冻结用户
            UserLockedTool.submit(()->{
                appUserLockedManager.setUserLocked(req.getUser().getId(), true);
            });
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_WALLET_WITHDRAW_017);
        }

        // 判断冻结用户
        if(appUserLockedManager.isLocked(req.getUser().getId())){
            throw new ApiServiceException(LetokenApiResponseEnum.ERROR_WALLET_WITHDRAW_017);
        }
        Integer tokenId = req.getTokenId();
        Token token = tokenMapper.selectById(tokenId);
        req.setToken(token);
        String uuid = getSeq("WD");
        LocalDateTime now = LocalDateTime.now();
        req.setUuId(uuid);
        req.setNow(now);
        return null;
    }
}
